/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.cx1.dao.api.ILeadDao;
import org.cx1.dao.impl.LeadDao;
import org.cx1.model.Lead;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.ILeadService;
import org.cx1.service.utils.IDGenerator;

/**
 *
 * @author ECH
 */
public class DLeadService implements ILeadService {

    private ILeadDao leadDao = new LeadDao();
    
    @Override
    public ServiceResponse<String> create(String name, String email, String description) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<String> create(Lead lead) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Lead entry = new Lead();
            id = IDGenerator.generateID();
            
            entry.setId(id);                        
            entry.setName(lead.getName());
            entry.setEmail(lead.getEmail());
            entry.setPhone(lead.getPhone());
                    
            entry.setTerm(lead.getTerm());
            entry.setType(lead.getType());
            
            entry.setCity(lead.getCity());
            entry.setNeighborhood(lead.getNeighborhood());
            entry.setStructure(lead.getStructure());
            
            entry.setBedsMin(lead.getBedsMin());
            entry.setBedsMax(lead.getBedsMax());
            entry.setPriceMin(lead.getPriceMin());
            entry.setPriceMax(lead.getPriceMax());
            
            entry.setDescription(lead.getDescription());
            
            entry.setActive(true);
            entry.setDateCreated(new Date());
            entry.setDateModified(new Date());
                        
            leadDao.add(entry);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<List<Lead>> readAll(int offset, int limit) {
        ServiceResponse<List<Lead>> response = new ServiceResponse<List<Lead>>();
        List<Lead> amenities = new ArrayList<Lead>();
        try {            
            List<Lead> records = leadDao.getAll();
            if (records != null && records.size() > 0) {
                amenities.addAll(records);
            }            
            response.setResponse(amenities);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> delete(String id) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            leadDao.deleteById(id);
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }
    
}
